import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';

class LoginTypeTow extends StatefulWidget {
  LoginTypeTow({super.key});

  @override
  State<LoginTypeTow> createState() => _SettingPageState();
}

class _SettingPageState extends State<LoginTypeTow> {
// class LoginType extends StatelessWidget {
  final RxBool _isLogin = true.obs;
  bool _obscureText = true;
  RxInt codetext = 60.obs;
  RxBool butdisabled = false.obs;

  RxBool valgroup = true.obs;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  final TextEditingController phone = TextEditingController(text: '');
  final TextEditingController code = TextEditingController();

  final TextEditingController password = TextEditingController(text: '');
  final TextEditingController password2 = TextEditingController(text: '');
  final TextEditingController inviteCode =
      TextEditingController(text: Get.parameters['invite_code']);

  @override
  void dispose() {
    code.dispose();
    phone.dispose();
    password.dispose();
    password2.dispose();
    inviteCode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.black12,
        ),
        extendBodyBehindAppBar: true,
        body: Column(
          children: [
            SafeArea(
              top: false,
              child: Image.asset(
                'static/images/dengli.png',
                fit: BoxFit.fitWidth,
              ),
            ),
            Container(
              height: 30,
              transform: Matrix4.translationValues(0.0, -15.0, 0.0),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(30),
                  color: Color(0XFFFFFFFF)),
            ),
            Expanded(
              child: ListView(
                children: [
                  Column(
                    children: [
                      Container(
                        transform: Matrix4.translationValues(0.0, -40.0, 0.0),
                        child: Image.asset(
                          'static/images/open.png',
                          fit: BoxFit.fitWidth,
                          width: 160,
                        ),
                      ),
                      Container(
                        margin:
                            EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 15,
                            ),
                            Image.asset(
                              'static/images/guoqi.png',
                              fit: BoxFit.fitWidth,
                              width: 30,
                            ),
                            SizedBox(
                              width: 5,
                            ),
                            Text('+86', style: TextStyle(fontSize: 14)),
                            Expanded(
                                child: TextField(
                              controller: phone,
                              style: TextStyle(fontSize: 14),
                              decoration: InputDecoration(
                                isCollapsed: true, //设置高度
                                contentPadding: EdgeInsets.all(15),
                                hintStyle: TextStyle(color: Color(0xff999999)),
                                border: InputBorder.none, // 隐藏输入框默认边框
                                hintText: '请输入手机号', // 提示文本
                              ),
                            ))
                          ],
                        ),
                      ),

                      Container(
                          margin: EdgeInsets.symmetric(
                              horizontal: 30, vertical: 10),
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom:
                                      BorderSide(color: Color(0XFFDDDDDD)))),
                          child: Row(
                            children: [
                              Expanded(
                                child: TextField(
                                  controller: code,
                                  style: TextStyle(fontSize: 14),
                                  decoration: InputDecoration(
                                    isCollapsed: true, //设置高度
                                    contentPadding: EdgeInsets.all(15),
                                    hintStyle:
                                        TextStyle(color: Color(0xff999999)),
                                    border: InputBorder.none, // 隐藏输入框默认边框
                                    hintText: '请输入验证码', // 提示文本
                                  ),
                                ),
                              ),
                              GestureDetector(
                                onTap: () {
                                  if (phone.text.isNotEmpty) {
                                    if (butdisabled.value) {
                                      return;
                                    }
                                    Get.find<HTTPS>().postData('/phone-code', {
                                      "phone": phone.text,
                                      "value": "reg",
                                    }).then((res) {
                                      if (res != null) {
                                        Get.find<HTTPS>().showTost('发送成功');
                                        butdisabled.value = true;
                                        codetext.value = 60;

                                        Timer.periodic(Duration(seconds: 1),
                                            (timer) {
                                          codetext.value--;
                                          if (codetext.value < 1) {
                                            codetext.value = 1;
                                            timer.cancel();
                                            butdisabled.value = false;
                                          }

                                          // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                                        });
                                      }
                                    });
                                  } else {
                                    Get.find<HTTPS>().showTost('输入手机号');
                                  }
                                },
                                child: Obx(() => butdisabled.value
                                    ? Text('已发送$codetext',
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0XFF0A72D9)))
                                    : Text('发送验证码',
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0XFF0A72D9)))),
                              ),
                            ],
                          )),

                      Container(
                          margin: EdgeInsets.symmetric(horizontal: 30),
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom:
                                      BorderSide(color: Color(0XFFDDDDDD)))),
                          child: TextField(
                            obscureText: true,
                            controller: password,
                            style: TextStyle(fontSize: 14),
                            decoration: InputDecoration(
                              isCollapsed: true, //设置高度
                              contentPadding: EdgeInsets.all(15),
                              hintStyle: TextStyle(color: Color(0xff999999)),
                              border: InputBorder.none, // 隐藏输入框默认边框
                              hintText: '请输入密码', // 提示文本
                            ),
                          )),
                      Container(
                          margin: EdgeInsets.only(left: 30, right: 30, top: 10),
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom:
                                      BorderSide(color: Color(0XFFDDDDDD)))),
                          child: TextField(
                            obscureText: true,
                            controller: password2,
                            style: TextStyle(fontSize: 14),
                            decoration: InputDecoration(
                              isCollapsed: true, //设置高度
                              contentPadding: EdgeInsets.all(15),
                              hintStyle: TextStyle(color: Color(0xff999999)),
                              border: InputBorder.none, // 隐藏输入框默认边框
                              hintText: '确认密码', // 提示文本
                            ),
                          )),
                      Container(
                          margin: EdgeInsets.only(left: 30, right: 30, top: 10),
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom:
                                      BorderSide(color: Color(0XFFDDDDDD)))),
                          child: TextField(
                            controller: inviteCode,
                            style: TextStyle(fontSize: 14),
                            decoration: InputDecoration(
                              isCollapsed: true, //设置高度
                              contentPadding: EdgeInsets.all(15),
                              hintStyle: TextStyle(color: Color(0xff999999)),
                              border: InputBorder.none, // 隐藏输入框默认边框
                              hintText: '输入邀请码（选填）', // 提示文本
                            ),
                          )),

                      GestureDetector(
                        onTap: () {

                          
                          if (!phone.text.isNotEmpty) {
                            Get.find<HTTPS>().showTost('输入手机号');
                            return;
                          }
                          if (!code.text.isNotEmpty) {
                            Get.find<HTTPS>().showTost('请输入验证码');
                            return;
                          }
                          if (!password.text.isNotEmpty) {
                            Get.find<HTTPS>().showTost('请输入密码');
                            return;
                          }
                          if (password.text != password2.text) {
                            Get.find<HTTPS>().showTost('密码不一致');
                            return;
                          }
                          if (password.text.length < 8) {
                            Get.find<HTTPS>().showTost('密码长度小于8');
                            return;
                          }

                          Get.find<HTTPS>().postData('/reg', {
                            'phone': phone.text,
                            'password': password.text,
                            'password2': password2.text,
                            'phone_code': code.text,
                            'invite_code': inviteCode.text
                          }).then((res) {
                            if (res != null) {
                              Get.find<HTTPS>().showTost('注册成功');

                               Timer(Duration(seconds: 1), () {
                                  Get.offNamed('/login/login_type');
                                });
                                
                            }
                          });
                        },
                        child: Container(
                            margin: EdgeInsets.only(top: 30),
                            width: 300,
                            height: 50,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular((50)),
                              gradient: LinearGradient(
                                colors: [
                                  Color(0xff2189E7),
                                  Color(0xffBF67F8),
                                ], // 定义颜色列表
                                begin: Alignment.topLeft, // 渐变开始的位置
                                end: Alignment.bottomRight, // 渐变结束的位置
                              ),
                            ),
                            child: Text('注 册',
                                style: TextStyle(
                                    fontSize: 16, color: Color(0XFFFFFFFF)))),
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text('已有账户？ 请', style: TextStyle(fontSize: 14)),
                          GestureDetector(
                            onTap: () {
                              Get.toNamed('/login/login_type');
                            },
                            child: Text('登录',
                                style: TextStyle(
                                    fontSize: 14, color: Color(0XFF0A72D9))),
                          ),
                        ],
                      ),
                      SizedBox(height: 20,),
                      // Container(
                      //   margin: EdgeInsets.only(left: 100, right: 100, top: 50),
                      //   child: Row(
                      //     mainAxisAlignment: MainAxisAlignment.spaceAround,
                      //     children: [
                      //       Image.asset(
                      //         'static/images/weixin.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //       Image.asset(
                      //         'static/images/pingguo.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //     ],
                      //   ),
                      // ),
                    ],
                  ),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Container(
                      width: 12,
                      height: 12,
                      margin: EdgeInsets.only(right: 3),
                      child: Obx(() => Image.asset(
                            valgroup.value
                                ? 'static/web/shous.png'
                                : 'static/web/shou.png',
                            width: 12,
                            height: 12,
                            fit: BoxFit.fitWidth,
                          ))),
                ),
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Text('已阅读并同意', style: TextStyle(fontSize: 11)),
                ),
                GestureDetector(
                  onTap: () {
                    Get.toNamed('/agreement?type=1', arguments: {'type': '1'});
                  },
                  child: Text('《用户服务协议》',
                      style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
                ),
                Text('与', style: TextStyle(fontSize: 11)),
                GestureDetector(
                  onTap: () {
                    Get.toNamed('/agreement?type=2', arguments: {'type': '1'});
                  },
                  child: Text('《隐私权政策》',
                      style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
                ),
              ],
            ),
            SizedBox(
              height: 15,
            )
          ],
        ));
  }
}
